var apiHost = wx.getStorageSync("apiHost"); //请求地址
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

var AppidFlag = wx.getStorageSync('AppidFlag');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // array: ['银行卡', '微信钱包'],
    // index: ''
    array: ['银行卡'],
    arrayBank: [],        //银行名称集合
    bankList:[],          //银行对象集合
    index: '0',           //选择器
    indexBank: '',        //选择器
    balance : 0 ,         //余额
    showFeeModel :false , //提示手续费
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var phone = wx.getStorageSync('phone');
    var token = wx.getStorageSync('token');

    that.setData({
      phone: phone,
      token: token
    })
    that.getBalance();
    that.getbank();
  },

  /**
   * 获取 银行
   */
  getbank : function(){
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/withDrawals/bank/list", //仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          var bank = res.data.data;
          var arrayBank = that.data.arrayBank;
          for(var i = 0 ; bank.length > i ; i ++){
            arrayBank.push(bank[i].name)
          }
          that.setData({
            arrayBank: arrayBank,
            bankList: bank
          })
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 获取用户余额
   */
  getBalance :function(){
    var that = this;
    var token = that.data.token
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/users/balance", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          that.setData({
            balance : res.data.data.balance
          })
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 跳转到提现详情
   */
  goPutForwardDetail:function(){
    wx.navigateTo({
      url: "../putForwardDetail/putForwardDetail",
    })
  },


  goRemind:function(){
    wx.navigateTo({
      url: "../putForwardRemind/putForwardRemind",
    })
  },
  /**
   * 根据收款方式进行跳转
   */
  bindPickerChange: function (e) {
    var that = this;
    // var index = e.detail.value
    // that.setData({
    //   index: index
    // })
    // if(index == 0){
    //   wx.navigateTo({
    //     url: "../../bankCard/selectBank/selectBank",
    //   })
    // }
  },

  /**
   * 选择银行卡pike
   */
  bindPickerChangeBank: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var that = this;
    var bankList = that.data.bankList
    var code = bankList[e.detail.value].code
   
    this.setData({
      indexBank: e.detail.value,
      code: code
    })
  },

  /**
   * 表单提交
   */
  formSubmit: function (e) {
    var that = this;
    var formData = e.detail.value;
    console.log(formData)
    if (formData.cardName == "" || formData.phone == "" || formData.cardNo == "" || formData.bandCode == "" || formData.amount == ""){
      that.showMsg("请完善信息")
    }else{
      that.setData({
        formData: formData,
        showFeeModel : true
      })
      that.getFee(formData.amount);//获取提现手续费
    }
  },

  /**
   * 获取提现手续费
   */
  getFee: function (amount){
    var that = this;
    var token = that.data.token
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/withDrawals/wx/fee", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        amount: amount
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        if (code == "10000") {
          var feeData = res.data.data;
          that.setData({
            amountFlag: feeData.amount,
            feeFlag: feeData.fee
          })
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 清空提现金额
   */
  clearAmount:function(){
    var that = this;
    that.setData({
      amount:''
    })
  },

  /**
   * 全部提现
   */
  allAmount : function(){
    var that = this;
    var balance = that.data.balance
    that.setData({
      amount: balance
    })
  },

  /**
   * 取消框
   */
  cancel :function(){
    this.setData({
      showFeeModel :false
    })
  },

  /**
   * 确定提现
   */
  cancelorders:function(){
    var that = this;
    var token = that.data.token;
    var formData = that.data.formData;
    console.log(formData)
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/withDrawals/wx/apply", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        cardName: formData.cardName,
        cardNo: formData.cardNo,
        bandCode: formData.bandCode,
        phone: formData.phone,
        amount: formData.amount,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        
        if (code == "10000") {
          that.goPutForwardDetail();  //跳转到提现详情页
          that.setData({
            showFeeModel: false
          })
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 提示信息
   */
  showMsg: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    });
  },
})